2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_COLOUREDITORCOMPONENT_JUCEHEADER__
27 #define __JUCER_COLOUREDITORCOMPONENT_JUCEHEADER__
30 //==============================================================================
32 A component that shows a colour swatch with hex ARGB value, and which pops up
33 a colour selector when you click it.
35 class ColourEditorComponent
: public Component
,
39 ColourEditorComponent (const bool canResetToDefault_
)
40 : canResetToDefault (canResetToDefault_
)
44 void paint (Graphics
& g
)
46 g
.fillAll (Colours::grey
);
48 g
.fillCheckerBoard (getLocalBounds().reduced (2, 2),
50 Colour (0xffdddddd).overlaidWith (colour
),
51 Colour (0xffffffff).overlaidWith (colour
));
53 g
.setColour (Colours::white
.overlaidWith (colour
).contrasting());
54 g
.setFont (getHeight() * 0.6f
, Font::bold
);
55 g
.drawFittedText (colour
.toDisplayString (true),
56 2, 1, getWidth() - 4, getHeight() - 1,
57 Justification::centred
, 1);
60 virtual void setColour (const Colour
& newColour
) = 0;
61 virtual void resetToDefault() = 0;
62 virtual const Colour
getColour() const = 0;
66 const Colour
col (getColour());
75 void mouseDown (const MouseEvent
& e
)
77 ColourSelectorComp
colourSelector (this, canResetToDefault
);
80 m
.addCustomItem (1234, &colourSelector
, 300, 400, false);
84 void changeListenerCallback (ChangeBroadcaster
* source
)
86 const ColourSelector
* const cs
= (const ColourSelector
*) source
;
88 if (cs
->getCurrentColour() != getColour())
89 setColour (cs
->getCurrentColour());
94 bool canResetToDefault
;
96 class ColourSelectorComp
: public Component
,
100 ColourSelectorComp (ColourEditorComponent
* owner_
,
101 const bool canResetToDefault
)
103 defaultButton ("Reset to Default")
105 addAndMakeVisible (&selector
);
106 selector
.setName ("Colour");
107 selector
.setCurrentColour (owner
->getColour());
108 selector
.addChangeListener (owner
);
110 if (canResetToDefault
)
112 addAndMakeVisible (&defaultButton
);
113 defaultButton
.addListener (this);
119 if (defaultButton
.isVisible())
121 selector
.setBounds (0, 0, getWidth(), getHeight() - 30);
122 defaultButton
.changeWidthToFitText (22);
123 defaultButton
.setTopLeftPosition (10, getHeight() - 26);
127 selector
.setBounds (0, 0, getWidth(), getHeight());
131 void buttonClicked (Button
*)
133 owner
->resetToDefault();
135 selector
.setCurrentColour (owner
->getColour());
139 class ColourSelectorWithSwatches
: public ColourSelector
142 ColourSelectorWithSwatches()
146 int getNumSwatches() const
148 return StoredSettings::getInstance()->swatchColours
.size();
151 const Colour
getSwatchColour (int index
) const
153 return StoredSettings::getInstance()->swatchColours
[index
];
156 void setSwatchColour (int index
, const Colour
& newColour
) const
158 StoredSettings::getInstance()->swatchColours
.set (index
, newColour
);
162 ColourEditorComponent
* owner
;
163 ColourSelectorWithSwatches selector
;
164 TextButton defaultButton
;
169 #endif // __JUCER_COLOUREDITORCOMPONENT_JUCEHEADER__